import 'package:flutter/material.dart';
import 'utils.dart';
import 'base_layout.dart';

/// 页面部局
class PageLayout extends BaseLayout {
  //滚动方向
  final Axis scrollDirection;
  //是否反转显示
  final bool reverse;
  // 控制器
  final PageController controller;
  //页面捕捉
  final bool pageSnapping;
  //滚动的动画表现
  final ScrollPhysics physics;
  //页面切换事件
  final ValueChanged<int> onPageChanged;
  //子组件集合，与builder属性只用一个
  final List<Widget> children;
  //子组件创建器，与children属性只用一个
  final IndexedWidgetBuilder builder;
  //子组件个数，只用于builder
  final int itemCount;

  PageLayout(
      {Key key,
      AlignmentGeometry alignment,
      double width,
      double height,
      bool isVisible,
      Decoration decoration,
      EdgeInsetsGeometry padding,
      EdgeInsetsGeometry margin,
      this.children = const [],
      this.scrollDirection = Axis.vertical,
      this.controller,
      this.reverse = false,
      this.onPageChanged,
      this.pageSnapping=true,
      this.physics,
      this.builder,
      this.itemCount})
      : assert((builder != null && itemCount != null) || children.length > 0),
        super(
            key: key,
            alignment: alignment,
            width: width,
            height: height,
            isVisible: isVisible,
            padding: padding,
            margin: margin,
            decoration: decoration);
  @override
  _PageLayoutState createState() => _PageLayoutState();
}

class _PageLayoutState extends State<PageLayout> with BaseState {
  @override
  void initState() {
    baseLayout = widget;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return buildState(buildContent);
  }

  @override
  Widget buildContent() {
    return PageView.custom(
        scrollDirection: widget.scrollDirection,
        pageSnapping: widget.pageSnapping,
        controller: widget.controller,
        reverse: widget.reverse,
        onPageChanged: widget.onPageChanged,
        physics: widget.physics,
        childrenDelegate: SliverChildBuilderDelegate(
            widget.builder == null
                ? (context, index) {
                    return widget.children[index];
                  }
                : widget.builder,
            childCount: widget.builder == null
                ? widget.children.length
                : widget.itemCount));
  }
}
